package engine.scene.player
{
	import engine.GameData;
	import engine.data.ModelLocator;
	import engine.data.SceneObject;
	import engine.flag.GameDefine;
	import engine.flag.PlayerActionFlag;
	import engine.logic.walk.NormalMoveLogic;
	import engine.newUi.FactionAttackLogic;
	import engine.scene.SceneManager;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.geom.Point;
	
	import newx.com.ResManager;
	import newx.com.config.StaticIni;
	import newx.utils.FilterUtil;
	
	public class PlayerModel extends BasePlayerModel
	{
		private var _sitPlace	: Sprite;
		
		private var _sitEffect	: MovieClip;
		
		protected var _challengeMc	: MovieClip;
		
		protected var _petModel	: PetModel;
		
		public function PlayerModel()
		{
			super();
			
			_moveLogic = new NormalMoveLogic();
			
			_sitPlace = new Sprite();
			addChild( _sitPlace );
		}
		/**
		 *	获取玩家站位点坐标
		 * @param playerObj
		 * @return point
		 * 
		 */
		override protected function getPosPoint( playerObj : SceneObject ):Point
		{
			var point : Point = new Point();
			
			var sceneManager : SceneManager = SceneManager.getInstance();
			
			var sceneType : int = GameData.SceneType;
			
			if( playerObj.CheckProperties( "BeforeEntryFBX" ) && sceneType == GameDefine.SceneType_Normal )
			{
				point.x	=	playerObj.GetProperties("BeforeEntryFBX");
				point.y	=	playerObj.GetProperties("BeforeEntryFBY");
				
				if( point.x != 0 || point.y != 0 )
				{
					return point;
				}
			}
			
			if( playerObj.CheckProperties( "PerScene" ) && playerObj.GetProperties( "PerScene" ) != 0 )
			{//有上个场景
				var curScene 	: int 		=	GameData.SceneModel
				var posObj		: Object 	=	StaticIni.getIniObj("SceneTP_" + curScene);		
				var perScene	: String	=	playerObj.GetProperties("PerScene");
				var node 		: Array		=	[];
				if( posObj == null )
				{//说明，坐标随即
					if( sceneType == GameDefine.SceneType_WorldBossScene )
					{
						var standPoint : Array =  sceneManager.walkPlacePoints[ "wait" ].split( "," );
						point.x = int( standPoint[0] ) + int( standPoint[2] * Math.random() );
						point.y = int( standPoint[1] ) + int( standPoint[3] * Math.random() );
						
						return point;
					}
					if( sceneType ==GameDefine.SceneType_FactionScene)
					{
						var areaName:String="";
						
						if(playerObj.GetProperties("GuildName") == FactionAttackLogic.getInstance().LeftFaction)
						{
							areaName = "leftwait";
						}
						else if(playerObj.GetProperties("GuildName") == FactionAttackLogic.getInstance().RihtFaction)
						{
							areaName = "rightwait";
						}
						
						var standPoint1 : Array =  sceneManager.walkPlacePoints[areaName].split( "," );
						point.x = int( standPoint1[0] ) + int( standPoint1[2] * Math.random() );
						point.y = int( standPoint1[1] ) + int( standPoint1[3] * Math.random() );
						
						return point;
					}
					var obj : Object = StaticIni.getIniObj("config_pos");
					point.x = obj.x;
					point.y = obj.y;
					
					return point;
					
				}else if( !posObj.hasOwnProperty( perScene ) )
				{
					node  = ( posObj["StandPoint"] as String ).split( "," );
					point.x = node[0];
					point.y = node[1];
					return point;		
					
				}else if( posObj.hasOwnProperty( perScene ) )
				{
					node = posObj[perScene].split( "," );
					point.x = node[0];
					point.y = node[1];
					
					return point;		
				}
			}
			
			point.x	=	playerObj.GetProperties("PlayerX");
			var limitY:int	=	sceneManager.walkPlacePoints[ "normal" ].split( "," )[1];
			point.y	=	playerObj.GetProperties("PlayerY")<limitY?limitY:playerObj.GetProperties("PlayerY");
			
			return point;
		}
		
		override public function set direction(value:int):void
		{
			super.direction = value;
			
			if( null != _petModel )
			{
				_petModel.direction = direction;
			}
		}
		
		override protected function updataPlayerInfo( playerObj : SceneObject ):void
		{
			if( null == playerObj ) 
			{
				return;
			}
			var canVisible:Boolean = (playerObj.GetPropertiesEx( "CantVisible" ) != 1);
			if(!canVisible)return;
			super.updataPlayerInfo( playerObj );
			
			
			updataPlayerChange(playerObj );
			
			worldPlayDead = playerObj.GetPropertiesEx( "WorldPlayerDead" );
			if(GameData.SceneType==GameDefine.SceneType_ForbiddenAreaScene) return;
			if(GameData.SceneType==GameDefine.SceneType_WorldBossScene) 	return;
			if(GameData.SceneType==GameDefine.SceneType_FactionBossScene) 	return;
			if(GameData.SceneType==GameDefine.SceneType_SnowBallFightScene) return;
			followPet = playerObj.GetPropertiesEx( "FollowPet" );
			followPetName=playerObj.GetPropertiesEx( "FollowPetName" );
			followPetGrade = playerObj.GetPropertiesEx("FollowPetGrade");
			followPetLevel = playerObj.GetPropertiesEx("FollowPetLevel");
		}
		
		/**
		 *	更新宠物 
		 * 
		 */		
		public function updataPet(value:String):void
		{
			
			if( null == _petModel )
			{
				_petModel = new PetModel();
			}
			if(playerObj)
				_petModel.playerObj = playerObj;
			this.addChild( _petModel );
			
			_petModel.master = this;
			
			_petModel.model = value;
		}
		
		public function updataPlayerChange( playerObj : SceneObject ):void
		{
			if( playerObj == null || _playerMovie == null )
			{
				if( _challengeMc != null )
				{
					_challengeMc.visible = false;
				}
				return;
			}
			
			if( playerObj != null && 0 != playerObj.GetPropertiesEx( "ChallengeIndex") )
			{//有这个属性,说明是前3名,显示称号
				if( _challengeMc == null )
				{
					_challengeMc = ResManager.newResClass("CustomControl_0160") as MovieClip;
					_challengeMc.mouseChildren = _challengeMc.mouseEnabled = false;
					addChild( _challengeMc );
				}
				_challengeMc.gotoAndStop( playerObj.GetPropertiesEx( "ChallengeIndex") );
				_challengeMc.visible = true;
			}else
			{
				if( _challengeMc != null )
				{
					_challengeMc.visible = false;
				}
			}
			
			if( _challengeMc != null )
			{
				_challengeMc.y	=	_familyLabel.y - _challengeMc.height;
				_challengeMc.x	=	_playerMovie.overhead.x - _challengeMc.width * .5;
			}
		}
		
		public function sit():void
		{
			killMove();
			
			status = PlayerActionFlag.Sit;
		}
		
		override public function set status(value:String):void
		{
			_sitPlace.mouseChildren = _sitPlace.mouseEnabled = _sitPlace.visible =  ( value == PlayerActionFlag.Sit );
			if( _playerMovie != null && _playerMovie.currentLabel == value ) return;
			
			super.status = value;
			
			
			if( null == _sitEffect )
			{
				_sitEffect		=	ResManager.newResClass("effect_7300144") as MovieClip;
				_sitPlace.addChild( _sitEffect );
				( _sitEffect.movie as MovieClip ).stop();
			}
			
			_sitPlace.mouseChildren = _sitPlace.mouseEnabled = _sitPlace.visible =  ( value == PlayerActionFlag.Sit );
			
			if( _sitPlace.visible )
			{
				//				_spPlace.x = originPoint.x;
				//				_spPlace.y = originPoint.y;
				( _sitEffect.movie as MovieClip ).play();
				
			}else
			{
				( _sitEffect.movie as MovieClip ).stop();
			}
			
			if( null != _playerMovie )
			{
				if(_playerMovie.hasOwnProperty("overhead"))
				{
					var overheadpoint:Number = _playerMovie.overhead.y;
					_nameLabel.y = (GameData.SceneType==GameDefine.SceneType_ForbiddenAreaScene)?
						overheadpoint*0.5 - _nameLabel.height:
						overheadpoint - _nameLabel.height;
					
					_familyLabel.y = (GameData.SceneType==GameDefine.SceneType_ForbiddenAreaScene)?
						overheadpoint*0.5 - _familyLabel.height - _nameLabel.height + 4:
						overheadpoint - _familyLabel.height - _nameLabel.height + 4;
					
				}
				
				//				if( _nameLabel.y < - 150 && _sitPlace.visible )
				//				{//特殊处理暂时没找到原因
				//					_nameLabel.y += 5;
				//					_familyLabel.y = _nameLabel.y - _nameLabel.height + 4;
				//				}
				//				else if(playerObj &&playerObj.CheckProperties("ModelEx") && int(playerObj.GetProperties("ModelEx")) ==22019901 )
				//				{
				//					_nameLabel.y += 10;
				//					_familyLabel.y = _nameLabel.y - _nameLabel.height + 4;
				//				}
				//				trace(playerName,_nameLabel.y,_familyLabel.y)
				
				if( _challengeMc != null )
				{
					_challengeMc.y	=	_familyLabel.y - _challengeMc.height;
				}
			}
		}
		
		public function set worldPlayDead(val:int):void
		{
			if( val > 0 )
			{
				_playerPlace.filters		=	[FilterUtil.GrayColorFilter];
			}else
			{
				_playerPlace.filters		=	null;
			}
		}
		
		public function set followPetName(value:String):void
		{
			if(GameData.SceneType ==GameDefine.SceneType_ForbiddenAreaScene || GameData.SceneType ==GameDefine.SceneType_WorldBossScene
				|| GameData.SceneType ==GameDefine.SceneType_SnowBallFightScene)
				return;
			if(_petModel)
			{
				if(playerObj)
				{
					if(playerObj.GetPropertiesEx("Name")== ModelLocator.getInstance().scene.Role.getPlayerName())
					{
						value =StaticIni.getSwfLangStrVar("UIString_1000296",[value]);
					}
					else
					{
						value =StaticIni.getSwfLangStrVar("UIString_1000295",[value]);
					}
				}
				
				_petModel.petName =value;
			}
			
		}
		
		public function set followPetGrade(value:int):void
		{
			if(GameData.SceneType ==GameDefine.SceneType_ForbiddenAreaScene || 
				GameData.SceneType ==GameDefine.SceneType_WorldBossScene||
				GameData.SceneType ==GameDefine.SceneType_SnowBallFightScene)
				return;
			if(_petModel)
			{
				if(playerObj)
				{
					_petModel.petGrade = value;
				}
			}
		}
		
		public function set followPetLevel(value:int):void
		{
			if(GameData.SceneType ==GameDefine.SceneType_ForbiddenAreaScene || 
				GameData.SceneType ==GameDefine.SceneType_WorldBossScene
				||	GameData.SceneType ==GameDefine.SceneType_SnowBallFightScene)
				return;
			if(_petModel)
			{
				if(playerObj)
				{
					_petModel.petLevel = value;
				}
			}
		}
		
		public function set followPet(value:String):void
		{
			if(GameData.SceneType ==GameDefine.SceneType_ForbiddenAreaScene || GameData.SceneType ==GameDefine.SceneType_SnowBallFightScene || GameData.SceneType ==GameDefine.SceneType_WorldBossScene)
				return;
			if( "" == value || value =="0" || value == null) 
			{
				if( null != _petModel )
				{
					_petModel.clear();
				}
			}
			else
			{
				updataPet(value);
			}
		}
		
		override public function startMove( path : Array, callBackFunc : Function = null, pList : * = null ):void
		{
			if( null == _petModel )
			{
				super.startMove( path, callBackFunc, pList );
			}else
			{
				super.startMove( path, onComplete );
				
				function onComplete():void
				{
					if(null != callBackFunc )
					{
						if(null != pList)
						{			
							callBackFunc.call(null, pList);
						}
						else
						{
							callBackFunc.call();
						}
					}
					
					_petModel.chaseMove();
				}
			}
		}
		
		override public function move(xDistance:int, yDistance:int):void
		{
			super.move( xDistance, yDistance );
			
			if( null != _petModel )
			{
				var xPoint : int = -direction * _petModel.width;
				
				if( -1 == direction && _petModel.x < xPoint || 1 == direction && _petModel.x > xPoint )
				{
					if( yDistance == 0 )
					{
						_petModel.status = PlayerActionFlag.Sit;
					}else
					{
						_petModel.status = PlayerActionFlag.Run;
					}
					
					_petModel.move( -xDistance, 0 );
				}else
				{
					_petModel.status = PlayerActionFlag.Run;
				}
			}
		}
		
		override public function reset():void
		{
			super.reset();
			
			if( null != _petModel )
			{
				_petModel.clear();
			}
		}
		
		override public function setVisible( visible : Boolean ):void
		{
			super.setVisible(visible);
		}
	}
}